import {
  reqGetHotKeywordList,
  reqGetSearchKeywordList,
  reqGetSearchShopInfo,
} from '@/api/search';

const state = {
  hotKeywordList: [],
  searchKeywordList: [],
  searchShopInfo: {},
};

const mutations = {
  SET_HOT_KEY_WORD_LIST(state, hotKeywordList) {
    state.hotKeywordList = hotKeywordList;
  },
  SET_SEARCH_KEYWORD_LIST(state, searchKeywordList) {
    state.searchKeywordList = searchKeywordList;
  },
  SET_SEARCH_SHOP_INFO(state, searchShopInfo) {
    state.searchShopInfo = searchShopInfo;
  },
};

const actions = {
  // 获取实时热搜关键词
  async getHotKeywordList({ commit }) {
    const result = await reqGetHotKeywordList();
    if (result.code == 200) {
      commit('SET_HOT_KEY_WORD_LIST', result.data.hotKeywordVOList);
    }
  },

  // 获取搜索关键词
  async getSearchKeywordList({ commit }, keyword) {
    const result = await reqGetSearchKeywordList(keyword);
    if (result.code == 200) {
      commit('SET_SEARCH_KEYWORD_LIST', result.data);
      return result.data;
    } else {
      return Promise.reject('failed');
    }
  },

  // 搜索获取商品信息
  async getSearchShopInfo(
    { commit },
    {
      keyword,
      descSorted = false,
      _stat_search = '',
      categoryId = 0,
      floorPrice = -1,
      upperPrice = -1,
    }
  ) {
    // console.log(
    //   keyword,
    //   descSorted,
    //   _stat_search,
    //   categoryId,
    //   floorPrice,
    //   upperPrice
    // );
    const result = await reqGetSearchShopInfo(
      keyword,
      descSorted,
      _stat_search,
      categoryId,
      floorPrice,
      upperPrice
    );
    if (result.code == 200) {
      commit('SET_SEARCH_SHOP_INFO', result.data);
      return 'ok';
    } else {
      return Promise.reject('failed');
    }
  },
};

const getters = {
  // 右边分类数据
  categoryList() {
    return state.searchShopInfo.categoryL1List || [];
  },

  // 搜索的商品列表数据
  searchShopList(state) {
    return state.searchShopInfo.directlyList || [];
  },

  // 搜索结果是否为空
  machType(state) {
    return state.searchShopInfo.matchType;
  },

  // 是否筛选为空
  hasMore(state) {
    return state.searchShopInfo.hasMore;
  },
};

export default {
  state,
  mutations,
  actions,
  getters,
};
